//
//  PopupMenuView.swift
//  HoneyBee
//
//  Created by Smith_Yang on 2018/5/14.
//  Copyright © 2018 point. All rights reserved.
//

import UIKit

protocol PopupMenuDelegate : class {
    func selectedIndex(index: Int)
}

class PopupMenuView: UITableView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    public var menuDelegate: PopupMenuDelegate?
    
    var menuIcons: [[String:Any]]?
    var menuTitles: [String]?
    
    init(frame: CGRect, imageIcons:[[String:Any]]?, title:[String]?) {
        super.init(frame: frame, style: UITableView.Style.plain)
        
        if let icons = imageIcons {
            menuIcons = icons
        }
        
        if let texts = title {
            menuTitles = texts
        }
        
        backgroundColor = UIColor(hex: "#efefef")
        
        layer.cornerRadius = 5
//        layer.backgroundColor = UIColor(hex: "#DDDDDD")?.cgColor

//        let shadowLayer = CALayer()
//        shadowLayer.frame = layer.bounds
//        shadowLayer.shadowColor = UIColor.black.cgColor
//        shadowLayer.shadowOpacity = 1
//        shadowLayer.shadowOffset = CGSize(width: -5, height: -5)
//        shadowLayer.shadowRadius = 5
//        layer.addSublayer(shadowLayer)
        
        dataSource = self
        delegate = self
        
        self.register(UINib(nibName: "PopupMenuTableViewCell", bundle: nil), forCellReuseIdentifier: "cell_identifier")
        self.separatorStyle = .none
        self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension PopupMenuView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let texts = menuTitles {
            return texts.count
        }
        
        if let icons = menuIcons {
            return icons.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell_identifier") as? PopupMenuTableViewCell else {return UITableViewCell()}
        if let iconItem = menuIcons?[indexPath.row] {
            let image = UIImage.icon(from: iconItem["name"] as! String, color: iconItem["color"] as! UIColor, imageSize: CGSize(width: 13, height: 13))
            cell.iconImageView.image = image
        }
        
        if let title = menuTitles?[indexPath.row] {
            cell.labelTitle.text = title
        }
        cell.backgroundColor = UIColor.clear
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return bounds.size.height/CGFloat((menuTitles?.count)!)
    }
    
}

extension PopupMenuView: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if menuDelegate != nil {
            menuDelegate?.selectedIndex(index: indexPath.row)
        }
        self.removeFromSuperview();
    }
    
}
